home *** CD-ROM | disk | FTP | other *** search
/ Network CD 2 / Network CD - Volume 2.iso / programs / internet / tcp / amitcp / amitcp-api-22.lha / AmiTCP-2.2 / src / netlib / lineread.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-08-12  |  3.8 KB  |  181 lines

  1. /*
  2.  * lineread.c
  3.  *
  4.  * Author: Tomi Ollila <too@cs.hut.fi>
  5.  *
  6.  * This module is FREEWARE. Standard "NO WARRANTY" disclaimer applies.
  7.  *
  8.  * Created: Thu Jul 12 16:40:03 EET 1990 too
  9.  * Last modified: Tue Jul 13 18:05:49 1993 too
  10.  *
  11.  * $Id: lineread.c,v 1.3 1993/07/13 15:15:53 too Exp $
  12.  *
  13.  * HISTORY
  14.  * $Log: lineread.c,v $
  15.  * Revision 1.3  1993/07/13  15:15:53  too
  16.  * Made lineread.c compile without <sys/cdefs.h> file
  17.  *
  18.  * Revision 1.1  1993/06/16  16:43:52  too
  19.  * Initial revision
  20.  *
  21.  *
  22.  */
  23.  
  24. #include "lineread.h"
  25.  
  26. #ifdef AMIGA
  27. extern struct Library * SocketBase;
  28. #define READ(a, b, c) recv(a, b, c, 0)
  29. #if __SASC
  30. #include <proto/socket.h>
  31. #elif __GNUC__
  32. #include <inline/socket.h>
  33. #endif
  34. #else /* not AMIGA */
  35. #define READ(a, b, c) read(a, b, c)
  36. #endif /* AMIGA */
  37.  
  38. #ifndef NULL
  39. #define NULL 0
  40. #endif
  41.  
  42. #ifndef FALSE
  43. #define FALSE 0
  44. #endif
  45.  
  46. #ifndef TRUE
  47. #define TRUE 1
  48. #endif
  49.  
  50. #ifndef __CONCAT
  51. #if defined (__STDC__) || defined (__cplusplus)
  52. #define __CONCAT(x,y) x ## y
  53. #else
  54. #define __CONCAT(x,y) x/**/y
  55. #endif
  56. #endif /* __CONCAT not defined */  
  57.   
  58. #define RLP(field) __CONCAT(rl->rl_Private.rlp_,field)
  59.  
  60.  
  61. #if defined (__STDC__) || defined (__cplusplus)
  62.  
  63. int lineRead(struct LineRead * rl)
  64.  
  65. #else
  66.  
  67. int lineRead(rl)
  68.      struct LineRead * rl;
  69.  
  70. #endif
  71. {
  72.   int i;
  73.   
  74.   if (RLP(Bufpointer) == RLP(Howlong))
  75.  
  76.     if (RLP(Selected)) {
  77.  
  78.       if (RLP(Line_completed))
  79.     RLP(Startp) = RLP(Bufpointer) = 0;
  80.  
  81.       if ((i = READ(rl->rl_Fd,
  82.             RLP(Buffer) + RLP(Bufpointer),
  83.             RLP(Buffersize) - RLP(Bufpointer))) <= 0) {
  84.     /*
  85.      * here if end-of-file or on error. set Howlong == Bufpointer
  86.      * so if non-blocking I/O is in use next call will go to READ()
  87.      */
  88.     RLP(Howlong) = RLP(Bufpointer);
  89.     rl->rl_Line = NULL;            
  90.     return i;
  91.       }
  92.       else
  93.     RLP(Howlong) = RLP(Bufpointer) + i;
  94.     }
  95.     else /* Inform user that next call may block (unless select()ed) */ 
  96.       {
  97.     RLP(Selected) = TRUE;
  98.     return 0;
  99.       }
  100.   else /* Bufpointer has not reached Howlong yet. */
  101.     {
  102.       RLP(Buffer)[RLP(Bufpointer)] = RLP(Saved);
  103.       RLP(Startp) = RLP(Bufpointer);
  104.     }
  105.  
  106.   /*
  107.    * Scan read string for next newline.
  108.    */
  109.   while (RLP(Bufpointer) < RLP(Howlong))
  110.     if (RLP(Buffer)[RLP(Bufpointer)++] == '\n')
  111.       goto Skip;
  112.  
  113.   /*
  114.    * Here if Bufpointer == Howlong.
  115.    */
  116.   if (rl->rl_Lftype != RL_LFNOTREQ) {
  117.     RLP(Selected) = TRUE;
  118.  
  119.     if (RLP(Bufpointer) == RLP(Buffersize)) {
  120.       /*
  121.        * Here if Bufpointer reaches end-of-buffer.
  122.        */
  123.       if (RLP(Startp) == 0) { /* (buffer too short for whole string) */
  124.     RLP(Line_completed) = TRUE;
  125.     rl->rl_Line = RLP(Buffer);
  126.     RLP(Buffer)[RLP(Bufpointer)] = '\0';
  127.     return -1;
  128.       }
  129.       /*
  130.        * Copy partial string to start-of-buffer and make control ready for
  131.        * filling rest of buffer when next call to lineRead() is made
  132.        * (perhaps after select()).
  133.        */
  134.       for (i = 0; i < RLP(Buffersize) - RLP(Startp); i++)
  135.     RLP(Buffer)[i] = RLP(Buffer)[RLP(Startp) + i];
  136.       RLP(Howlong)-= RLP(Startp);
  137.       RLP(Bufpointer) = RLP(Howlong);
  138.       RLP(Startp) = 0;
  139.     }
  140.     
  141.     RLP(Line_completed) = FALSE;
  142.     return 0;
  143.   }
  144.  
  145.  Skip:
  146.   RLP(Line_completed) = TRUE;
  147.   if (rl->rl_Lftype == RL_LFREQNUL)
  148.     RLP(Buffer)[RLP(Bufpointer) - 1] = '\0';
  149.   RLP(Saved) = RLP(Buffer)[RLP(Bufpointer)];
  150.   RLP(Buffer)[RLP(Bufpointer)] = '\0';
  151.   RLP(Selected) = FALSE;
  152.   rl->rl_Line = RLP(Buffer) + RLP(Startp);
  153.  
  154.   return (RLP(Bufpointer) - RLP(Startp));
  155. }
  156.  
  157. #undef READ      
  158.  
  159. #if defined (__STDC__) || defined (__cplusplus)
  160.       
  161. void initLineRead(struct LineRead * rl, int fd, int lftype, int buffersize)
  162.       
  163. #else
  164.  
  165. int initLineRead(rl, fd, lftype, buffersize)
  166.       struct LineRead * rl;
  167.       int fd;
  168.       int lftype;
  169.       int buffersize;
  170.   
  171. #endif
  172. {      
  173.   rl->rl_Fd     = fd;
  174.   rl->rl_Lftype = lftype;
  175.  
  176.   RLP(Bufpointer) = RLP(Howlong) = 0;
  177.   RLP(Selected) = RLP(Line_completed) = TRUE;
  178.  
  179.   RLP(Buffersize) = buffersize;
  180. }
  181.